<template>
  <div class="search-container" ref="searchContainer">
    <!-- 搜索输入框 -->
    <div class="components-input-demo-presuffix">

      <a-input v-model:value="searchText" placeholder="" class="custom-input" @pressEnter="handleSearch"
        @focus="showHistory = true" style="background-color:var(--color-bg);  ">
        <template #prefix>

          <div class="searchBox" v-if="txet">
            <div class="searchItem">
              <div> {{ txet }}</div>
              <div>
                <img src="../../../assets/images/new_img/叉.png" alt="关闭" @click="handleclose" class="close">
              </div>
            </div>
          </div>
          <div style="padding-left: 0.52vw ;" v-else>
            <SearchOutlined style="color:var(--color-text);font-size: 1.1vw;" />
          </div>

        </template>
      </a-input>

    </div>

    <!-- 历史记录面板 -->
    <div v-show="showHistory && searchHistory.length" class="history-panel" style="background-color:var(--color-bg);">
      <div class="history-title" style=" color:var(--color-text)">最近搜索</div>
      <div class="history-items">
        <div v-for="(item, index) in searchHistory" :key="index" class="history-item" @click="selectHistory(item)">
          {{ item }}
          <CloseOutlined class="history-remove" @click.stop="removeHistory(index)" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { SearchOutlined, CloseOutlined } from '@ant-design/icons-vue';

import { useStore } from '@/store'
const { themeStore } = useStore()
const router = useRouter();
const route = useRoute();
const searchText = ref('');
const showHistory = ref(false);
const searchContainer = ref(null);
const searcShow = ref(true)
const txet = ref('')
const visible = ref()

const searchHistory = ref([
  'UI设计',
  'UI设计瀑布流',
  'UI设计项目复盘',
  '网页设计',
  '网页PC端简约',
  '网页正在升级中',
  '关于我们',
  '企业官网'
]);





watch(() => {
  txet.value = themeStore.searcShow.txet
  visible.value = themeStore.searcShow.visible
  console.log(themeStore.searcShow.visible, "vuex");



})
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onBeforeUnmount(() => {
  document.removeEventListener('click', handleClickOutside);
});


const handleClickOutside = (event) => {
  if (searchContainer.value && !searchContainer.value.contains(event.target)) {
    showHistory.value = false;
  }
};

const handleSearch = () => {
  if (searchText.value.trim()) {
    searchHistory.value = searchHistory.value.filter(item => item !== searchText.value);
    searchHistory.value.unshift(searchText.value);
    if (searchHistory.value.length > 8) searchHistory.value.pop();
    showHistory.value = false;
  }
};

const selectHistory = (item) => {
  searchText.value = item;
  showHistory.value = false;
};

const removeHistory = (index) => {
  searchHistory.value.splice(index, 1);
};

const handleclose = () => {
  themeStore.searcShow.txet = '';
  themeStore.searcShow.visible = !themeStore.searcShow.visible
}


</script>

<style lang="less" scoped>
.search-container {
  position: relative;
  width: 30vw;
  margin: 0 auto;
  margin-left: 1.2vw;

  .components-input-demo-presuffix {


    /* 动画效果 */
    .slide-fade-enter-active,
    .slide-fade-leave-active {
      transition: all 0.3s ease;
    }

    .slide-fade-enter-from {
      transform: translateX(20px);
      opacity: 0;
    }

    .slide-fade-leave-to {
      transform: translateX(-20px);
      opacity: 0;
    }






    .ant-input-affix-wrapper {
      padding: 0px !important;

    }

    .searchBox {
      width: 100%;
      height: 100%;
      text-align: center;
      border-radius: 2.6vw;
      border: 1px solid rgb(187, 187, 187);
      overflow: hidden;



      .searchItem {
        width: auto;
        height: 100%;
        background-color: var(--color-seabg);
        border-radius: 2.6vw;
        text-align: center;
        padding: 0px 15px;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        gap: 16px;
        font-size: 0.83vw;
        color: var(--color-search-t);


        .close {
          width: 0.52vw;
          height: 0.52vw;
          color: var(--color-search-t);
        }
      }
    }
  }
}


::v-deep .ant-input-affix-wrapper>input.ant-input {
  font-size: var(--text-16) !important;
  color: var(--color-search-t) !important;
  background-color: var(--color-seabg) !important;

}




.custom-input {
  border-radius: 2.6vw !important;
  height: 2.39vw !important;
  overflow: hidden;
  background-color: var(--color-seabg) !important;

  &:hover,
  &:focus,
  &:focus-within {
    border-color: #d9d9d9 !important;
    box-shadow: none !important;

    .ant-input-prefix {
      color: rgba(0, 0, 0, 0.25) !important;
      // background-color: rgb(187,187,187) !important;

    }
  }
}

.history-panel {
  position: absolute;
  top: 3.4vw;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid var(--border-ls);
  border-radius: 0.4vw;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  padding: 0.625vw 0.83vw;
  z-index: 1000;
}

.history-title {
  color: rgba(0, 0, 0, 0.45);
  font-size: 0.93vw;
  margin-bottom: 0.9vw;
}

.history-items {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0.52vw 0px;
}

.history-item {
  // width: 4.16vw;
  height: 2.08vw;
  background: var(--color-seabg);
  border-radius: 2.6vw;
  padding: 0 12px;
  font-size: 0.83vw;
  cursor: pointer;
  display: flex;
  align-items: center;
  color: var(--color-text);
  gap: 10px;
  transition: all 0.2s;


  &:hover {
    background: #e6e6e6;
  }
}

.history-remove {
  color: #999;
  font-size: 12px;

  &:hover {
    color: #ff4d4f;
  }
}
</style>